package com.example.administrator.zkdemo.adapter;

/**
 * Created with Android Studio.
 * 描述: ${DESCRIPTION}
 * Date: 2018/11/21
 * Time: 16:24
 *
 * @author 晏琦云
 * @version ${VERSION}
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.zkdemo.db.orm.tna.UserInfo;
import com.example.administrator.zkdemo.model.ZKEmployeeBean;
import com.example.administrator.zkdemo.utils.CircleTransform;
import com.zkteco.android.utils.ZKPhotoUtil;

import java.util.Iterator;
import java.util.List;

public class ZKEmployeeadapter extends RecyclerView.Adapter<ZKEmployeeadapter.ViewHolder> {
    private Context context;
    private boolean isDelete;
    private EditClick mClick;
    private List<ZKEmployeeBean> mDataList;
    private final LayoutInflater mInflater;

    public ZKEmployeeadapter(Context paramContext, List<ZKEmployeeBean> paramList) {
        this.context = paramContext;
        this.mDataList = paramList;
        this.mInflater = LayoutInflater.from(paramContext);
    }

    public List<ZKEmployeeBean> getData() {
        return this.mDataList;
    }

    public ZKEmployeeBean getItem(int paramInt) {
        return this.mDataList.get(paramInt);
    }

    public int getItemCount() {
        if (this.mDataList == null)
            return 0;
        return this.mDataList.size();
    }

    public void onBindViewHolder(final ViewHolder paramViewHolder, int paramInt) {
        final ZKEmployeeBean localZKEmployeeBean = getItem(paramInt);
        paramViewHolder.re_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View paramView) {
                if (ZKEmployeeadapter.this.mClick != null) {
                    if (ZKEmployeeadapter.this.isDelete) {
                        boolean bool2 = localZKEmployeeBean.isBol();
                        boolean bool1 = false;
                        if (!bool2) {
                            paramViewHolder.isCheck.setImageResource(R.drawable.item_checked);
                            localZKEmployeeBean.setBol(true);
                        } else {
                            paramViewHolder.isCheck.setImageResource(R.drawable.item_unchecked);
                            localZKEmployeeBean.setBol(false);
                        }
                        Iterator<ZKEmployeeBean> iterator = ZKEmployeeadapter.this.mDataList.iterator();
                        while (iterator.hasNext()) {
                            if (!iterator.next().isBol())
                                continue;
                            bool1 = true;
                        }
                        ZKEmployeeadapter.this.mClick.btnState(bool1);
                        return;
                    }
                    ZKEmployeeadapter.this.mClick.onClick(localZKEmployeeBean.getUserInfo());
                }
            }
        });
        paramViewHolder.userName.setText(localZKEmployeeBean.getUserInfo().getName());
        paramViewHolder.userID.setText(localZKEmployeeBean.getUserInfo().getUser_PIN());
        RequestOptions options = new RequestOptions()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .error(R.mipmap.ic_nul_photo)
                .transform(new BitmapTransformation[]{new CircleTransform(this.context)});
        Glide.with(this.context)
                .load(ZKPhotoUtil.getUserPhoto(localZKEmployeeBean.getUserInfo().getUser_PIN()))
                .apply(options)
                .into(paramViewHolder.userPhoto);
        if (this.isDelete)
            paramViewHolder.isCheck.setVisibility(0);
        else
            paramViewHolder.isCheck.setVisibility(8);
        if (localZKEmployeeBean.isBol()) {
            paramViewHolder.isCheck.setImageResource(R.drawable.item_checked);
            return;
        }
        paramViewHolder.isCheck.setImageResource(R.drawable.item_unchecked);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup paramViewGroup, int paramInt) {
        return new ViewHolder(this.mInflater.inflate(R.layout.employee_item_layout, paramViewGroup, false));
    }

    public void remove(int paramInt) {
        this.mDataList.remove(paramInt);
        notifyDataSetChanged();
    }

    public void setClick(EditClick paramEditClick) {
        this.mClick = paramEditClick;
    }

    public void setData(List<ZKEmployeeBean> paramList) {
        this.mDataList = paramList;
        notifyDataSetChanged();
    }

    public void setIsdelete(boolean paramBoolean) {
        this.isDelete = paramBoolean;
        if (!this.isDelete) {
            Iterator localIterator = this.mDataList.iterator();
            while (localIterator.hasNext())
                ((ZKEmployeeBean) localIterator.next()).setBol(false);
        }
        notifyDataSetChanged();
    }

    public static abstract interface EditClick {
        public abstract void btnState(boolean paramBoolean);

        public abstract void onClick(UserInfo paramUserInfo);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView isCheck;
        private LinearLayout re_all;
        private TextView userID;
        private TextView userName;
        private ImageView userPhoto;

        @SuppressLint({"WrongViewCast"})
        public ViewHolder(View view) {
            super(view);
            this.isCheck = view.findViewById(2131296422);
            this.userPhoto = view.findViewById(2131296648);
            this.userName = view.findViewById(2131296647);
            this.userID = view.findViewById(2131296645);
            this.re_all = view.findViewById(2131296490);
        }
    }
}
